/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.dao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javainfo.dao.inf.BaseDaoIf;

public abstract class BaseDao<T> extends HibernateDaoSupport implements
		BaseDaoIf<T> {

	/** entityClass */
	private Class<T> entityClass;

	/**
	 * 构造函数
	 */
	@SuppressWarnings("unchecked")
	public BaseDao() {
		entityClass = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}

	/**
	 * 根据Id查询
	 * 
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T getById(int id) {
		T obj = (T) getHibernateTemplate().get(entityClass, id);
		return obj;
	}

	/**
	 * 查询全部
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 * @throws DataAccessException
	 */
	@SuppressWarnings("unchecked")
	public List<T> getAll() {
		return getHibernateTemplate().loadAll(entityClass);
	}

	/**
	 * 获取数量
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 * @throws DataAccessException
	 */
	public int getListSize() {
		return getHibernateTemplate().loadAll(entityClass).size();
	}

	/**
	 * 追加
	 * 
	 * @param user
	 */
	public void add(T obj) {
		getHibernateTemplate().save(obj);
	}

	/**
	 * 追加复数个
	 * 
	 * @param user
	 */
	public void addAll(Collection<T> objs) {
		for (T t : objs) {
			getHibernateTemplate().save(t);
		}
	}

	/**
	 * 删除
	 * 
	 * @param user
	 */
	public void delete(T obj) {
		getHibernateTemplate().delete(obj);
	}

	/**
	 * 删除复数个
	 * 
	 * @param userList
	 */
	public void deleteAll(Collection<T> list) {
		getHibernateTemplate().deleteAll(list);
	}

	/**
	 * 更新数据
	 * 
	 * @param user
	 */
	public void update(T obj) {
		getHibernateTemplate().update(obj);
	}

	/**
	 * 更新复数个数据
	 * 
	 * @param user
	 */
	public void updateAll(Collection<T> objs) {
		getHibernateTemplate().update(objs);
	}

	/**
	 * 按条件查询
	 * 
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 */
	@SuppressWarnings("unchecked")
	public List<T> getByConditions(Collection<ConditionMap> conditions)
			throws InstantiationException, IllegalAccessException,
			SecurityException, NoSuchMethodException, IllegalArgumentException,
			InvocationTargetException {
		T t = entityClass.newInstance();
		for (ConditionMap condition : conditions) {
			Method method = entityClass.getMethod(condition.getTheMethodName(),
					entityClass);
			method.invoke(t, condition.getTheValue());
		}
		return getHibernateTemplate().findByExample(t);
	}

	/**
	 * 按条件查询 传入的对象的属性值查询
	 * 
	 * @param t
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> getByTemp(T t) {
		return getHibernateTemplate().findByExample(t);
	}

	/**
	 * 获取分页数据
	 * 
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	@SuppressWarnings("unchecked")
	public List<T> pageByConditions(T t, int start, int limit)
			throws InstantiationException, IllegalAccessException {
		return getHibernateTemplate().findByExample(t, start, limit);
	};

	/**
	 * 获取分页数据
	 * 
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	@SuppressWarnings("unchecked")
	public List<T> getByPage(int start, int limit)
			throws InstantiationException, IllegalAccessException {
		T obj = entityClass.newInstance();
		return getHibernateTemplate().findByExample(obj, start, limit);
	}
}
